package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonUpdItemController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonItemRepository itemRepository;

    @Autowired
    YxyContinueduLessonRepository lessonRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/upd/item", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        String name = reqModel.name;
        YxyContinueduLessonItem item = itemRepository.findById(id);
        Assert.isTrue(item != null, "未获取课时");
        item.setTitle(name);
        String lessonId = item.getLessonId();

        YxyContinueduLesson lesson = lessonRepository.findById(lessonId);
        if (lesson != null) {
            lesson.setLastUpDate(new Date());
            lessonRepository.save(lesson);
        }

        itemRepository.save(item);
    }

    public static class ReqModel extends YxyApiReqModel {
        public String id;
        public String name;

        @Override
        public void autoCheck() {

        }
    }
}
